package com.crm.system.service.impl.returnMoney;

import com.crm.common.model.DhPageInfo;
import com.crm.model.qo.returnMoney.ModifyRoyaltyStatusLogQO;
import com.crm.model.vo.returnMoney.ModifyRoyaltyStatusLogVO;
import com.crm.service.returnMoney.ModifyRoyaltyStatusLogDbService;
import com.crm.system.service.returnMoney.ModifyRoyaltyStatusLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/11/26 15:24
 */
@Service
public class ModifyRoyaltyStatusLogServiceImpl implements ModifyRoyaltyStatusLogService {
    @Autowired
    private ModifyRoyaltyStatusLogDbService modifyRoyaltyStatusLogDbService;

    /**
     * 财务 - 回款管理 - 到款记录 - “修改回款提成发放状态日志”列表
     * @Author pym
     * @Date  2021/11/26 16:05
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param modifyRoyaltyStatusLogQO -筛选条件
     * @return {@link {com.crm.common.model.DhPageInfo<com.crm.model.vo.returnMoney.ModifyRoyaltyStatusLogVO>}}
     */
    @Override
    public DhPageInfo<ModifyRoyaltyStatusLogVO> selectModifyRoyaltyStatusLogList(Integer pageIndex, Integer pageSize,
                                                                                 String sortName, String sortOrder,
                                                                                 ModifyRoyaltyStatusLogQO modifyRoyaltyStatusLogQO) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序字符串拼接
        String order = getOrder(sortName, sortOrder);
        List<ModifyRoyaltyStatusLogVO> modifyRoyaltyStatusLogVOList = modifyRoyaltyStatusLogDbService
                .selectModifyRoyaltyStatusLogList(order, modifyRoyaltyStatusLogQO);
        PageInfo<ModifyRoyaltyStatusLogVO> pageInfo = new PageInfo<ModifyRoyaltyStatusLogVO>(modifyRoyaltyStatusLogVOList);
        return new DhPageInfo<ModifyRoyaltyStatusLogVO>(pageInfo,modifyRoyaltyStatusLogVOList);
    }

    /**
     * 排序字符串拼接
     * @Author pym
     * @Date  2021/8/19 10:45
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @return {@link {java.lang.String}}
     */
    private String getOrder(String sortName, String sortOrder) {
        String order = "";
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch(sortName){
                case "contractNumber":
                { //合同编号
                    order = getOrder(order, sortOrder, "contract_number");
                }
                break;
                case "customerName":
                { //客户名称
                    order = getOrder(order, sortOrder, "customer_name");
                }
                break;
                case "orderNumber":
                { //订单编号
                    order = getOrder(order, sortOrder, "order_number");
                }
                break;
                case "productName":
                { //产品名称
                    order = getOrder(order, sortOrder, "product_name");
                }
                break;
                case "arriveTime":
                { //到款时间
                    order = getOrder(order, sortOrder, "arrive_time");
                }
                break;
                case "arriveMoney":
                { //到款金额
                    order = getOrder(order, sortOrder, "arrive_money");
                }
                break;
                case "arriveNum":
                { //到款编号
                    order = getOrder(order, sortOrder, "arrive_num");
                }
                break;
                case "scoreConvertRatio":
                { //业绩换算比例
                    order = getOrder(order, sortOrder, "score_convert_ratio");
                }
                break;
                case "scoreMoney":
                { //业绩金额
                    order = getOrder(order, sortOrder, "score_money");
                }
                break;
                case "arriveUnitName":
                { //到款单位名称
                    order = getOrder(order, sortOrder, "arrive_unit_name");
                }
                break;
                case "modifyBefore":
                { //提成发放状态修改前
                    order = getOrder(order, sortOrder, "modify_before");
                }
                break;
                case "modifyAfter":
                { //提成发放状态修改后
                    order = getOrder(order, sortOrder, "modify_after");
                }
                break;
                case "operateTime":
                { //操作时间
                    order = getOrder(order, sortOrder, "operate_time");
                }
                break;
                case "operatePeopleName":
                { //操作人
                    order = getOrder(order, sortOrder, "operate_people_name");
                }
                break;
                default:
                    break;
            }
        }
        if (!order.contains("create_by")) {
            order = getOrder(order, "DESC", "create_by");
        }
        return order;
    }
    /**
     * 排序字符串拼接
     * @Author pym
     * @Date  2021/8/19 10:45
     * @param order -排序字符串
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @return {@link {java.lang.String}}
     */
    private String getOrder(String order, String sortOrder, String sortName) {
        if (StringUtils.isBlank(order)) {
            order += sortName +" "+ sortOrder;
        } else {
            order += ","+sortName +" " + sortOrder;
        }
        return order;
    }

}
